using System;
using System.Data;
using System.Collections;
using APP.QUANLY.Common;
using RMAS.DAL;

namespace RMAS.DAL
{
    /// <summary>
    /// Summary description for QT_QUANTITATIVE.
    /// </summary>
    public class QT_QUANTITATIVEDao
    {
        private string _SQLSelectByKey;
        private string _SQLUpdate;
        private string _SQLInsert;
        private string _SQLDelete;
        private string _SQLSelectAll;

        #region Properties

        public IDbConnection _dbConnection;

        public IDbConnection DbConnection
        {
            get { return _dbConnection; }
            set { _dbConnection = value; }
        }

        #endregion

        #region Constructor

        public QT_QUANTITATIVEDao()
        {
            _dbConnection = null;
            InitSqlQuery();
        }

        public QT_QUANTITATIVEDao(IDbConnection con)
        {
            _dbConnection = con;
            InitSqlQuery();
        }

        #endregion


        private void ToLowerQuery()
        {
            _SQLDelete = _SQLDelete.ToLower();
            _SQLUpdate = _SQLUpdate.ToLower();
            _SQLInsert = _SQLInsert.ToLower();
            _SQLSelectByKey = _SQLSelectByKey.ToLower();
        }

        private void ToUpperQuery()
        {
            _SQLDelete = _SQLDelete.ToUpper();
            _SQLUpdate = _SQLUpdate.ToUpper();
            _SQLInsert = _SQLInsert.ToUpper();
            _SQLSelectByKey = _SQLSelectByKey.ToUpper();
        }

        #region InitSqlQuery
        private void InitSqlQuery()
        {
            string query;

            query = @"SELECT ID, PrID, MeterialID, MeterialName, UnitName, NumberMeterial, PriceMeterial, IDPOS FROM QT_QUANTITATIVE WHERE (ID = @ID)";
            _SQLSelectByKey = query;

            query = @"UPDATE QT_QUANTITATIVE SET PrID = @PrID, MeterialID = @MeterialID, MeterialName = @MeterialName, UnitName = @UnitName, NumberMeterial = @NumberMeterial, PriceMeterial = @PriceMeterial, IDPOS = @IDPOS WHERE (ID = @ID)";
            _SQLUpdate = query;

            query = @"INSERT INTO QT_QUANTITATIVE VALUES (@ID, @PrID, @MeterialID, @MeterialName, @UnitName, @NumberMeterial, @PriceMeterial, @IDPOS)";
            _SQLInsert = query;

            query = @"DELETE FROM QT_QUANTITATIVE WHERE (ID = @ID)";
            _SQLDelete = query;

            query = @"SELECT ID, PrID, MeterialID, MeterialName, UnitName, NumberMeterial, PriceMeterial, IDPOS FROM QT_QUANTITATIVE";
            _SQLSelectAll = query;
        }
        #endregion
        public void LoadByCommandText(string commandtext, IDbTransaction trans)
        {
            if (_dbConnection == null)
            {
                throw new Exception("Connection has not been set.");
            }

            IDbCommand command;

            try
            {
                if (_dbConnection.State != ConnectionState.Open) { _dbConnection.Open(); }

                command = _dbConnection.CreateCommand();
                command.CommandText = commandtext;

                if (trans != null) { command.Transaction = trans; }
                if (command.ExecuteNonQuery() == 0) { throw new Exception("Zero rows were affected."); }
            }
            catch (Exception ex) { throw ex; }
        }

        #region Save & Delete
        private void Save(QT_QUANTITATIVE obj, IDbTransaction trans, bool isExisting)
        {
            if (_dbConnection == null) { throw new Exception("Connection has not been set."); }

            IDbCommand command;

            try
            {
                if (_dbConnection.State != ConnectionState.Open) { _dbConnection.Open(); }
                command = _dbConnection.CreateCommand();

                if (!isExisting) { command.CommandText = _SQLInsert + ";"; }
                else { command.CommandText = _SQLUpdate + ";"; }

                DalTools.addDbDataParameter(command, "ID", obj.ID, DbType.Int64);
                DalTools.addDbDataParameter(command, "PrID", obj.PrID, DbType.String);
                DalTools.addDbDataParameter(command, "MeterialID", obj.MeterialID, DbType.String);
                DalTools.addDbDataParameter(command, "MeterialName", obj.MeterialName, DbType.String);
                DalTools.addDbDataParameter(command, "UnitName", obj.UnitName, DbType.String);
                DalTools.addDbDataParameter(command, "NumberMeterial", obj.NumberMeterial, DbType.Double);
                DalTools.addDbDataParameter(command, "PriceMeterial", obj.PriceMeterial, DbType.Double);
                DalTools.addDbDataParameter(command, "IDPOS", obj.IDPOS, DbType.String);

                if (trans != null) { command.Transaction = trans; }
                if (command.ExecuteNonQuery() == 0) { throw new Exception("Zero rows were affected."); }
            }
            catch (Exception ex) { throw ex; }
        }

        public void Delete(QT_QUANTITATIVE obj, IDbTransaction trans)
        {
            if (_dbConnection == null)
            {
                //throw new DalException("Connection has not been set.");
                throw new Exception("Connection has not been set.");
            }

            IDbCommand command;

            try
            {
                if (_dbConnection.State != ConnectionState.Open) { _dbConnection.Open(); }

                command = _dbConnection.CreateCommand();
                command.CommandText = _SQLDelete + ";";

                DalTools.addDbDataParameter(command, "ID", obj.ID, DbType.Int64);

                if (trans != null) { command.Transaction = trans; }
                if (command.ExecuteNonQuery() == 0) { throw new Exception("Zero rows were affected."); }
            }
            catch (Exception ex) { throw ex; }
        }

        public void Insert(QT_QUANTITATIVE obj, IDbTransaction trans)
        {
            Save(obj, trans, false);
        }
        public void Update(QT_QUANTITATIVE obj, IDbTransaction trans)
        {
            Save(obj, trans, true);
        }
        #endregion

        #region Command Execute & Read Properties

        private QT_QUANTITATIVEList SelectListByCommand(IDbCommand command)
        {
            QT_QUANTITATIVEList list = new QT_QUANTITATIVEList();
            if (_dbConnection == null) { throw new Exception("Connection has not been set."); }
            try
            {
                QT_QUANTITATIVE obj;
                if (_dbConnection.State != ConnectionState.Open) { _dbConnection.Open(); }
                using (IDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        obj = new QT_QUANTITATIVE();
                        ReadProperties(obj, reader);
                        list.Add(obj);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return list;
        }

        private QT_QUANTITATIVEList SelectListByCommand(IDbCommand command, IDbTransaction trans)
        {
            QT_QUANTITATIVEList list = new QT_QUANTITATIVEList();
            if (_dbConnection == null) { throw new Exception("Connection has not been set."); }
            try
            {
                QT_QUANTITATIVE obj;
                if (_dbConnection.State != ConnectionState.Open) { _dbConnection.Open(); }
                command.Transaction = trans;
                using (IDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        obj = new QT_QUANTITATIVE();
                        ReadProperties(obj, reader);
                        list.Add(obj);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return list;
        }

        private void DeleteListByCommand(IDbCommand command, IDbTransaction trans)
        {
            if (_dbConnection == null) { throw new Exception("Connection has not been set."); }
            try
            {
                if (_dbConnection.State != ConnectionState.Open) { _dbConnection.Open(); }
                if (trans != null) { command.Transaction = trans; }
                command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void ReadProperties(QT_QUANTITATIVE obj, IDataReader reader)
        {
            try
            {
                if (!(reader["ID"] is DBNull))
                {
                    obj.ID = (Int64)reader["ID"];
                }
                if (!(reader["PrID"] is DBNull))
                {
                    obj.PrID = (string)reader["PrID"];
                }
                if (!(reader["MeterialID"] is DBNull))
                {
                    obj.MeterialID = (string)reader["MeterialID"];
                }
                if (!(reader["MeterialName"] is DBNull))
                {
                    obj.MeterialName = (string)reader["MeterialName"];
                }
                if (!(reader["UnitName"] is DBNull))
                {
                    obj.UnitName = (string)reader["UnitName"];
                }
                if (!(reader["NumberMeterial"] is DBNull))
                {
                    obj.NumberMeterial = (double)reader["NumberMeterial"];
                }
                if (!(reader["PriceMeterial"] is DBNull))
                {
                    obj.PriceMeterial = (double)reader["PriceMeterial"];
                }
                if (!(reader["IDPOS"] is DBNull))
                {
                    obj.IDPOS = (string)reader["IDPOS"];
                }
            }
            catch (Exception ex)
            {
                throw new DalException("Failed to read properties from DataReader.", ex);
            }
        }
        #endregion

        public QT_QUANTITATIVE SelectByKey(Int64 ID)
        {
            IDbCommand command = _dbConnection.CreateCommand();
            command.CommandText = _SQLSelectByKey;
            DalTools.addDbDataParameter(command, "ID", ID, DbType.Int64);
            QT_QUANTITATIVEList list = SelectListByCommand(command);

            if (list.Count == 0) { throw new Exception("No data was returned"); }
            return list[0];
        }

        public QT_QUANTITATIVE SelectByKey(Int64 ID, IDbTransaction trans)
        {
            IDbCommand command = _dbConnection.CreateCommand();
            command.CommandText = _SQLSelectByKey;
            DalTools.addDbDataParameter(command, "ID", ID, DbType.Int64);
            QT_QUANTITATIVEList list = SelectListByCommand(command, trans);

            if (list.Count == 0) { throw new Exception("No data was returned"); }
            return list[0];
        }

        public QT_QUANTITATIVEList SelectAll()
        {
            IDbCommand command = _dbConnection.CreateCommand();
            if (!StaticVar.IsServer)
            {
                _SQLSelectAll = _SQLSelectAll + " WHERE IDPOS = @IDPOS";
                DalTools.addDbDataParameter(command, "IDPOS", StaticVar.CPUID, DbType.String);
            }
            command.CommandText = _SQLSelectAll;
            QT_QUANTITATIVEList list = SelectListByCommand(command);
            return list;
        }
        public QT_QUANTITATIVEList SelectAll(IDbTransaction trans)
        {
            IDbCommand command = _dbConnection.CreateCommand();
            if (!StaticVar.IsServer)
            {
                _SQLSelectAll = _SQLSelectAll + " WHERE IDPOS = @IDPOS";
                DalTools.addDbDataParameter(command, "IDPOS", StaticVar.CPUID, DbType.String);
            }
            command.CommandText = _SQLSelectAll;
            QT_QUANTITATIVEList list = SelectListByCommand(command, trans);
            return list;
        }
        public QT_QUANTITATIVEList SelectByCommand(string Command)
        {
            IDbCommand command = _dbConnection.CreateCommand();
            command.CommandText = Command;
            QT_QUANTITATIVEList list = SelectListByCommand(command);
            return list;
        }

        public QT_QUANTITATIVEList SelectByCondition(QT_QUANTITATIVE obj, IDbTransaction trans)
        {
            IDbCommand command = _dbConnection.CreateCommand();
            command.CommandText = "SELECT * FROM QT_QUANTITATIVE " + GetCondtionQuery(obj) + ";";
            FillParamToCommand(command, obj);
            QT_QUANTITATIVEList list = SelectListByCommand(command, trans);
            return list;
        }

        public QT_QUANTITATIVEList SelectByCondition(QT_QUANTITATIVE obj)
        {
            IDbCommand command = _dbConnection.CreateCommand();
            command.CommandText = "SELECT * FROM QT_QUANTITATIVE " + GetCondtionQuery(obj) + ";";
            FillParamToCommand(command, obj);
            QT_QUANTITATIVEList list = SelectListByCommand(command);
            return list;
        }
        private void DeleteByCondition(QT_QUANTITATIVE obj, IDbTransaction trans)
        {
            IDbCommand command = _dbConnection.CreateCommand();
            command.CommandText = "DELETE FROM QT_QUANTITATIVE " + GetCondtionQuery(obj) + ";";
            FillParamToCommand(command, obj);
            DeleteListByCommand(command, trans);
        }

        #region Make Condition Query & Fill Param from Condition to Command
        private string GetCondtionQuery(QT_QUANTITATIVE obj)
        {
            if (obj == null) { return ""; }
            string s = "";
            if (obj.IsSetID)
            {
                s += " AND ID = @ID ";
            }
            if (obj.IsSetPrID)
            {
                s += " AND PrID = @PrID ";
            }
            if (obj.IsSetMeterialID)
            {
                s += " AND MeterialID = @MeterialID ";
            }
            if (obj.IsSetMeterialName)
            {
                s += " AND MeterialName = @MeterialName ";
            }
            if (obj.IsSetUnitName)
            {
                s += " AND UnitName = @UnitName ";
            }
            if (obj.IsSetNumberMeterial)
            {
                s += " AND NumberMeterial = @NumberMeterial ";
            }
            if (obj.IsSetPriceMeterial)
            {
                s += " AND PriceMeterial = @PriceMeterial ";
            }
            if (obj.IsSetIDPOS)
            {
                s += " AND IDPOS = @IDPOS ";
            }
            if (s.Length > 0)
            {
                s = " WHERE " + s.Substring(4);
                return s;
            }
            return "";
        }

        private void FillParamToCommand(IDbCommand command, QT_QUANTITATIVE obj)
        {
            if (obj == null) { return; }
            if (obj.IsSetID)
            {
                DalTools.addDbDataParameter(command, "ID", obj.ID, DbType.Int64);
            }
            if (obj.IsSetPrID)
            {
                DalTools.addDbDataParameter(command, "PrID", obj.PrID, DbType.String);
            }
            if (obj.IsSetMeterialID)
            {
                DalTools.addDbDataParameter(command, "MeterialID", obj.MeterialID, DbType.String);
            }
            if (obj.IsSetMeterialName)
            {
                DalTools.addDbDataParameter(command, "MeterialName", obj.MeterialName, DbType.String);
            }
            if (obj.IsSetUnitName)
            {
                DalTools.addDbDataParameter(command, "UnitName", obj.UnitName, DbType.String);
            }
            if (obj.IsSetNumberMeterial)
            {
                DalTools.addDbDataParameter(command, "NumberMeterial", obj.NumberMeterial, DbType.Double);
            }
            if (obj.IsSetPriceMeterial)
            {
                DalTools.addDbDataParameter(command, "PriceMeterial", obj.PriceMeterial, DbType.Double);
            }
            if (obj.IsSetIDPOS)
            {
                DalTools.addDbDataParameter(command, "IDPOS", obj.IDPOS, DbType.String);
            }
        }
        #endregion
    }
}